<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_offlineapi/model')->方法名();
*/
namespace jy_offlineapi;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function add_log($order=array()){
		foreach($order['goods'] as $item){
			$goods=pdo_get('jy_offlineapi_goods',array('goods_id'=>$item['goodsid']));
			if($goods['id']){
				$api_sid=pdo_getcolumn('jy_weishop_store',array('id'=>$order['storeid']),'api_sid');
				$indata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'goods_id'=>$goods['goods_id'],
					'order_id'=>$order['id'],
					'api_gid'=>$goods['api_gid'],
					'api_sid'=>$api_sid,
					'ispay'=>0,
					'status'=>0,
					'createtime'=>TIMESTAMP,
				);
				$this->insert('goods_log',$indata);
			}
		}
		return true;
	}
	public function send_api($order=array()){
		$order['id']=$order['orderid']?:$order['id'];
		$logs=pdo_getall('jy_offlineapi_goods_log',array('order_id'=>$order['id']));
		foreach($logs as &$lv){
			$goods=pdo_get('jy_offlineapi_goods',array('goods_id'=>$lv['goods_id']));
			$lv['salerid']=$order['salerid'];
			$lv['verifycode']=$order['verifycode'];
			$lv['type']=$order['verifynum']?'goods':'order';
			if($goods['id']){
				if($goods['type']==1){
					$this->api_buygoods($lv);
					return true;
				}
				if($goods['type']==0){
					$this->api_openmember($lv);
					return true;
				}
			}
		}
		return false;
	}
	public function api_openmember($log=array()){
		$member=model('core/member')->detail($log['mid'],array('credit1','credit2','mobile','avatar','nickname'));
		$order=pdo_get('jy_weishop_order',array('id'=>$log['order_id']));
		if($log['salerid']){
			$saler = pdo_get('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$log['salerid']));
		}
		if(!$saler['id']){
			//$saler = pdo_get('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$order['storeid']));
		}
		if(!$saler['id']){
			//$saler = pdo_get('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid']));	
		}
		$post=array(
			'sCode'=>$log['api_sid'],
			'phone'=>$member['mobile'],
			'phoneName'=>$member['nickname'],
			'packageId'=>$log['api_gid'],
			'oNum'=>$order['ordersn'],
			'oPrice'=>$order['price'],
			'oTime'=>$order['paytime'],
			'mCode'=>$saler['mobile'],
			'mName'=>$saler['name'],
			'verifycode'=>$log['verifycode'],
			'type'=>$log['type'],
		);
		$cloud=$this->plugin_setting('basic');
		if(!$cloud['apiurl']){
			exi('请先设置《接口地址》！','error');
		}
		$this->api_request($cloud['openurl'],$post);
		return true;
	}
	public function api_buygoods($log=array()){
		return true;
	}
	public function api_login($api_sid=0,$mobile=0){
		$cloud=$this->plugin_setting('basic');
		if(!$cloud['apiurl']){
			exi('请先设置《接口地址》！','error');
		}
		$post=array(
			'sCode'=>$api_sid,
			'mCode'=>$mobile,
		);
		$this->api_request($cloud['loginurl'],$post);
		return true;
	}
	public function goods_verify($goods_verify=array()){
		
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'verifygoodsid'=>$goods_verify['id'],
			'salerid'=>$clerk_id,
			'storeid'=>$goods_verify['storeid'],
			'verifynum'=>$num,
			'remarks'=>$remarks,
			'verifydate'=>time(),
		);
		$this->pdo_insert('verifygoods_log',$log);
		$logs=pdo_count('jy_weishop_verifygoods_log',array('verifygoodsid'=>$goods_verify['id']),'id');
		if($logs==$goods_verify['limitnum'] && $goods_verify['limitnum']>0){
				$this->order_receive($goods_verify['orderid']);
		}
		return true;
	}
	//对接第三方
	function api_request($apiurl,$post=array(),$direct=false,$passerror=false){
		global $_AR;
		$cloud=$this->plugin_setting('basic');
		if(!$cloud['apiurl']){
			exi('请先设置《接口地址》！','error');
		}
		$secret=$cloud['apikey'];
		$cloud_url=$apiurl?:$cloud['apiurl'];
		$token=http_build_query($post);
		$token=$this->model('des')->encrypt($token);
		$cloud_url .='?token='.$token;
		exi('','',$cloud_url);
		header('location:'.$cloud_url);exit;
		$res=cfc('http')->ihttp_request($cloud_url);
		$bak=$res['content'];
		if($direct){
			return $bak;
		}
		$result=@unserialize(base64_decode($bak));
		if(is_array($result)){
			if($result['type']=='error'){
				exi($result['message'],'warning');
			}elseif($result['type']=='success'){
				return $result['message'];
			}
			if(!$passerror){
				exi('连接第三方接口失败!','warning');
			}
		}
		if(!$passerror){
			exi('连接第三方接口失败.','warning');
		}
	}
}
?>